package org.apache.rocketmq.console.script.service.impl;

import java.util.List;

import org.apache.rocketmq.console.config.RMQConfigure;
import org.apache.rocketmq.console.script.dao.EnvInfoDao;
import org.apache.rocketmq.console.script.dao.EnvironDicDao;
import org.apache.rocketmq.console.script.dao.RegionInfoDao;
import org.apache.rocketmq.console.script.dao.ScriptInfoDao;
import org.apache.rocketmq.console.script.dao.SysInfoDao;
import org.apache.rocketmq.console.script.model.EnvInfo;
import org.apache.rocketmq.console.script.model.EnvironDic;
import org.apache.rocketmq.console.script.model.RegionInfo;
import org.apache.rocketmq.console.script.model.ScriptInfo;
import org.apache.rocketmq.console.script.model.SysInfo;
import org.apache.rocketmq.console.script.service.EnvService;
import org.apache.rocketmq.console.shiro.entity.UserInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EnvServiceImpl implements EnvService {
	@Autowired
	private RMQConfigure rmqConfigure;
	@Autowired
	private ScriptInfoDao scriptInfoDao;

	@Autowired
	private EnvironDicDao environDicDao;

	@Autowired
	private SysInfoDao sysInfoDao;

	@Autowired
	private RegionInfoDao regionInfoDao;

	@Autowired
	private EnvInfoDao envInfoDao;

	@Override
	public List<ScriptInfo> findAllScript() {
		List<ScriptInfo> scriptInfos = scriptInfoDao.findAll();
		for (ScriptInfo scriptInfo : scriptInfos) {
			EnvironDic environDic = environDicDao.findOne(scriptInfo.getEnvId());
			if (environDic != null) {
				scriptInfo.setEnviroCode(environDic.getEnviroCode());
				scriptInfo.setEnviroName(environDic.getEnviroName());
				scriptInfo.setRegionCode(environDic.getRegionCode());
				scriptInfo.setRegionName(environDic.getRegionName());
				scriptInfo.setNameserver(environDic.getNameserver());
			}
		}
		return scriptInfos;
	}

	@Override
	public List<SysInfo> findAllSysInfo() {
		List<SysInfo> findAll = sysInfoDao.findAll();
		return findAll;
	}

	@Override
	public List<EnvironDic> findAllEnvironDic() {
		return environDicDao.findAll();
	}

	@Override
	public void save(ScriptInfo entity) {
		scriptInfoDao.save(entity);

	}

	@Override
	public EnvironDic findEnvironDicByRegionCodeAndEnviroCode(String regionCode, String enviroCode) {
		return environDicDao.findOneByRegionCodeAndEnviroCode(regionCode, enviroCode);
	}

	@Override
	public List<EnvironDic> findAllEnvironDicByRegionCode(String regionCode) {
		return environDicDao.findByRegionCode(regionCode);
	}

	@Override
	public ScriptInfo findScriptInfoById(int id) {

		return scriptInfoDao.findOneById(id);
	}

	@Override
	public EnvironDic findOneEnvironDic(int id) {
		return environDicDao.findOne(id);
	}

	@Override
	public String findSysNameBySysCode(String code) {
		return sysInfoDao.findOneByCode(code).getName();
	}

	@Override
	public List<EnvironDic> findAllDic() {
		UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();

		EnvironDic environDic = rmqConfigure.getCurrentRegion();
		int permissionId = userInfo.getPermissionId();
		// 查询到所有的数据
		if (environDic == null || permissionId == 1) {
			return environDicDao.findAll();
		} else {
			return environDicDao.findByRegionCode(environDic.getRegionCode());
		}

	}

	@Override
	public EnvironDic findOneByNameserveLike(String nameserve) {
		return environDicDao.findOneByNameserverLike("%" +nameserve+"%");
	}

	@Override
	public List<RegionInfo> findAllRegionInfo() {
		return regionInfoDao.findAll();
	}

	@Override
	public List<EnvInfo> findAllEnvInfo() {
		return envInfoDao.findAll();
	}

	@Override
	public boolean saveEnvironDic(EnvironDic entity) {
		try {
			environDicDao.save(entity);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	@Override
	public String findReginNameByRegionCode(String code) {
		return regionInfoDao.findOneByCode(code).getName();
	}

	@Override
	public String findEnvNameByEnvCode(String code) {
		return envInfoDao.findOneByCode(code).getName();
	}

	@Override
	public RegionInfo findRegionInfoByCode(String regionCode) {
		return regionInfoDao.findOneByCode(regionCode);
	}

	@Override
	public boolean saveRegionInfo(RegionInfo entity) {
		try {
			regionInfoDao.save(entity);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

}
